package com.schoolMall.interceptor;

import com.schoolMall.domain.Shop;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Objects;

/**
 * @author:zyh
 * @Time:2021-09-04-17:00
 * @email:1269231889@qq.com
 */
public class ShopPermissionInterceptor extends HandlerInterceptorAdapter {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        Shop currentShop = (Shop) request.getSession().getAttribute("currentShop");
        List<Shop> shopList = (List<Shop>) request.getSession().getAttribute("shopList");
        if (Objects.isNull(currentShop) || CollectionUtils.isNotEmpty(shopList)) {
            return false;
        }
        for (Shop shop : shopList) {
            if (currentShop.getShopId().equals(shop.getShopId())) {
                return true;
            }
        }
        return super.preHandle(request, response, handler);
    }
}
